package com.example.fw;

import java.util.ArrayList;
import java.util.List;

public class ContactHelper extends HelperBase{

	private static int TIMEOUT = 5000;
	
	public ContactHelper(ApplicationManager applicationManager) {
		super(applicationManager);
	}

	private List<Contact> cashedContacts;
	
	public ContactHelper createContact(Contact contact) {
		initContactCreation();
		fillContactForm(contact);
		confirmContactCreation();
		return this;
	}

	public ContactHelper deleteContact(int index) {
		confirmContactDeletion(index);
		return this;
	}

	private void initContactCreation() {
		manager.getAutoItHelper()
			.winWaitAndActivate("AddressBook Portable", "", TIMEOUT)
			.click("TRbButton4")
			.winWaitAndActivate("Add Contact", "", TIMEOUT);
	}
	
	private void fillContactForm(Contact contact) {
		manager.getAutoItHelper()
			.send("TDBEdit12", contact.firstname)
			.send("TDBEdit11", contact.lastname);
	}
	
	private void confirmContactCreation() {
		manager.getAutoItHelper()
			.click("TRbButton1")
			.winWaitAndActivate("AddressBook Portable", "", TIMEOUT);
		cashedContacts = null;
	}
	
	private void confirmContactDeletion(int index) {
		selectContact(index);
		manager.getAutoItHelper()
		.click("TRbButton2")
		.winWaitAndActivate("Confirm", "", TIMEOUT)
		.click("TButton2");
		cashedContacts = null;
	}
	
	public List<Contact> getContacts() {
		if (cashedContacts == null) {
			rebuildCashOfContacts();
		}
		return cashedContacts;
	}
	
	public void rebuildCashOfContacts() {
		cashedContacts = new ArrayList<Contact>();
		manager.getAutoItHelper()
			.winWaitAndActivate("AddressBook Portable", "", TIMEOUT)
			.click("TListView1");
		for (int i = 0; i < 100; i++) {
			selectContact(i + 1);
			manager.getAutoItHelper().click("Edit");
			if (manager.getAutoItHelper().isWindowPresent("Information")) {
				manager.getAutoItHelper()
				.winWaitAndActivate("Information", "", TIMEOUT)
				.click("TButton1");
				break;
			}
			manager.getAutoItHelper().winWaitAndActivate("Update Contact", "", TIMEOUT);
			Contact contact = new Contact()
				.setFirstname(manager.getAutoItHelper().getText("TDBEdit12"))
				.setLastname(manager.getAutoItHelper().getText("TDBEdit11"));
			manager.getAutoItHelper()
				.click("Cancel")
				.winWaitAndActivate("AddressBook Portable", "", TIMEOUT);
			if (i != 0) {
				if (contact.equals(cashedContacts.get(i - 1))) {
					break;
				}
			}
			cashedContacts.add(contact);
		}
	}
	
	private void selectContact(int index) {
		String downKeys = "";
		for (int i = 0; i < index; i++) {
			downKeys = downKeys.concat("{DOWN}");
		}
		manager.getAutoItHelper().send(downKeys + "{SPACE}");
	}
	
	/*
	public Contact getContact(int index) {
		manager.getAutoItHelper()
			.winWaitAndActivate("AddressBook Portable", "", 5000)
			.click("TListView1");
		selectContact(index);
		manager.getAutoItHelper()
			.click("Edit")
			.winWaitAndActivate("Update Contact", "", 5000);
		Contact contact = new Contact()
			.setFirstname(manager.getAutoItHelper().getText("TDBEdit12"))
			.setLastname(manager.getAutoItHelper().getText("TDBEdit11"));
		manager.getAutoItHelper()
			.click("Cancel")
			.winWaitAndActivate("AddressBook Portable", "", 5000);
		return contact;
	}
	*/
}
